package com.zhilei.controller;

import com.zhilei.base.BaseInfoProperties;
import com.zhilei.grace.result.GraceJSONResult;
import com.zhilei.intercept.JWTCurrentUserInterceptor;
import com.zhilei.pojo.Admin;
import com.zhilei.pojo.bo.CreateAdminBO;
import com.zhilei.pojo.bo.UpdateAdminBO;
import com.zhilei.pojo.bo.UpdateAdminPwdBO;
import com.zhilei.pojo.vo.AdminVO;
import com.zhilei.service.AdminService;
import com.zhilei.untils.PagedGridResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 接收前端请求的数据的请求体，使用 @RequestBody 这会将请求的正文，就是请求的JSON转换为Java对象
 * 返回给前端数据，也就是将数据写入到JSON中发送给前端，使用 @ResponseBody
 *
 * @author zhilei
 * @data 2024-09-05
 */
@RestController
@RequestMapping("admininfo")
@Slf4j
@Api
public class AdminInfoController extends BaseInfoProperties {

    @Autowired
    private AdminService adminService;

    /**
     * 列表分页查询
     *
     * @param accountName
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("list")
    public GraceJSONResult list(@RequestParam(value = "accountName") String accountName,
                                Integer page,
                                Integer limit) {

        // TODO 获取 admin 列表
        PagedGridResult adminListPaged = adminService.getAdminListPaged(accountName, page, limit);
        return GraceJSONResult.ok(adminListPaged);

    }

    /**
     * 运营平台创建admin账号
     *
     * @param createAdminBO
     * @return
     */
    @PostMapping("create")
    public GraceJSONResult create(@Validated @RequestBody CreateAdminBO createAdminBO) {

        // TODO 创建管理员账号
        adminService.createAdmin(createAdminBO);

        return GraceJSONResult.ok();
    }

    /**
     * 删除管理员账号
     *
     * @param username
     * @return
     */
    @PostMapping("delete")
    public GraceJSONResult delete(@RequestParam("username") String username) {

        // TODO 删除管理员账号
        adminService.deleteAdmin(username);

        return GraceJSONResult.ok();
    }

    /**
     * 更新密码
     *
     * @param updateAdminPwdBO
     * @return
     */
    @PostMapping("resetPwd")
    public GraceJSONResult resetPwd(@Validated @RequestBody UpdateAdminPwdBO updateAdminPwdBO) {

        // TODO 修改管理员密码
        adminService.updateAdminPwdBO(updateAdminPwdBO);

        return GraceJSONResult.ok();
    }

    /**
     * 运营平台查询admin账号信息
     *
     * @return
     */
    @PostMapping("myInfo")
    public GraceJSONResult myInfo() {

        // TODO 查询管理员信息
        Admin admin = JWTCurrentUserInterceptor.ADMIN_THREAD.get();
        // TODO 2. 获取 admin 信息,重新查询最新的信息
        Admin adminInfo = adminService.getById(admin.getId());
        AdminVO adminVO = new AdminVO();
        BeanUtils.copyProperties(adminInfo, adminVO);

        return GraceJSONResult.ok(adminVO);
    }


    /**
     * 运营平台更新admin账号信息
     *
     * @param updateAdminBO
     * @return
     */
    @PostMapping("updateMyInfo")
    public GraceJSONResult updateMyInfo(@Validated @RequestBody UpdateAdminBO updateAdminBO) {

        // TODO 更新管理员的用户名 备注 图片
        adminService.updateMyInfo(updateAdminBO);

        return GraceJSONResult.ok();
    }
}






















